package edu.cmu.abi;

import java.util.ArrayList;
import java.util.Iterator;
import edu.cmu.abi.track.Track;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author SimplyYogi
 */
public class SimilarityCalculator {

    public static  double calcArtistSimilarity(Artist artistA, Artist artistB){

        ArrayList<Track> songListA=artistA.getListOfSongs();
        ArrayList<Track> songListB=artistB.getListOfSongs();
        return calcSongListSimilarity(songListA, songListB);

    }
    public static double calcSongListSimilarity(ArrayList<Track> songListA, ArrayList<Track> songListB) {

        double similarityScore = 0.0;
        double cumulativeScore = 0.0;
        Iterator itA = songListA.listIterator();

        while (itA.hasNext()) {
            Track songA = (Track) itA.next();
            Iterator itB = songListB.listIterator();

            while (itB.hasNext()) {
                Track songB = (Track) itB.next();
                cumulativeScore = +calcCosine(songA, songB);
            }
        }

        similarityScore = cumulativeScore / (songListA.size() * songListB.size());
        return similarityScore;
    }

    public static double calcCosine(double[] feature1, double[] feature2) {

        double sum_x_square = 0.0;
        double sum_y_square = 0.0;
        double x_y = 0.0;

        for (double i : feature1) {
            sum_x_square += i * i;

        }

        for (double i : feature2) {
            sum_y_square += i * i;
        }

        for (int i = 0; i < feature1.length; i++) {
            x_y += feature1[i] * feature2[i];
        }

        System.out.println("numerator:" + x_y);

        double cosine = x_y / (Math.sqrt(sum_x_square) * Math.sqrt(sum_y_square));

        return cosine;

    }

    public static double calcCosine(Track songA, Track songB) {

//        double[] sp_mean_A = songA.getSegments_pitch_mean();
//        double[] sp_var_A = songA.getSegments_pitch_var();
//        double[] st_mean_A = songA.getSegments_timbre_mean();
//        double[] st_var_A = songA.getSegments_timbre_var();
//
//        double[] sp_mean_B = songB.getSegments_pitch_mean();
//        double[] sp_var_B = songB.getSegments_pitch_var();
//        double[] st_mean_B = songB.getSegments_timbre_mean();
//        double[] st_var_B = songB.getSegments_timbre_var();
//
//        double sum_x_square = 0;
//        double sum_y_square = 0;
//
//
//        //here, we calculate the cosine distance between the two tracks
//
//        for (double i : sp_mean_A) {
//            sum_x_square += i * i;
//        }
//        System.out.println("x_square" + sum_x_square);
//
//        for (double j : sp_var_A) {
//            sum_x_square += j * j;
//        }
//
//        System.out.println(sum_x_square);
//
//        for (double k : st_mean_A) {
//            sum_x_square += k * k;
//        }
//
//        System.out.println(sum_x_square);
//
//        for (double l : st_var_A) {
//            sum_x_square += l * l;
//        }
//
//        System.out.println(sum_x_square);
//        System.out.println("---------------------");
//
//        for (double i : sp_mean_B) {
//            sum_y_square += i * i;
//        }
//
//        System.out.println("y_square:" + sum_y_square);
//
//        for (double j : sp_var_B) {
//            sum_y_square += j * j;
//        }
//
//        System.out.println(sum_y_square);
//        for (double k : st_mean_B) {
//            sum_y_square += k * k;
//        }
//
//        System.out.println(sum_y_square);
//        for (double l : st_var_B) {
//            sum_y_square += l * l;
//        }
//
//        System.out.println(sum_y_square);
//        System.out.println("---------------------");
//        /* Three different cases
//         * if target context ID and candidate context ID are equal, we multiply their frequency
//         * if target context ID is less than candidate's, since context IDs are sorted, we can skip the whole iteration
//         * if target context ID is greater than candidate's, we move to the next candidate context ID
//         */
//
//        double x_y = 0.0;
//
//        for (int i = 0; i < sp_mean_A.length; i++) {
//            x_y += sp_mean_A[i] * sp_mean_B[i];
//        }
//
//        System.out.println("x_y:" + x_y);
//
//        for (int i = 0; i < sp_var_A.length; i++) {
//            x_y += sp_var_A[i] * sp_var_B[i];
//        }
//
//        System.out.println(x_y);
//
//        for (int i = 0; i < st_mean_A.length; i++) {
//            x_y += st_mean_A[i] * st_mean_B[i];
//        }
//
//        System.out.println(x_y);
//
//        for (int i = 0; i < st_var_A.length; i++) {
//            x_y += st_var_A[i] * st_var_B[i];
//        }
//
//        System.out.println("numerator:" + x_y);
//
//        double cosine = x_y / (Math.sqrt(sum_x_square) * Math.sqrt(sum_y_square));
//
//        return cosine;
            return 0.0;

    }
}
